export class Url {
  /**
   * 跳转路径
   * @param segment
   */
  static replace(segment: IUrlSegment | string) {
    let url: string;
    if (typeof segment == "string") {
      url = segment;
    } else {
      url = this.build(segment);
    }
    window.location.hash = url;
  }
  /**
   * 生成 URL HASH 部分
   * @param segment
   */
  static build(segment: IUrlSegment): string {
    let r = "#" + segment.menuLevel.toString();
    if (typeof segment.pagePath == "undefined") {
      return r;
    }
    r += `/${segment.pagePath}`;

    // if (typeof segment.action != "undefined" && segment.action != UrlAction.other) {
    //     r += `/action_${segment.action}`
    // }

    if (typeof segment.query == "undefined") {
      return r;
    }
    const keys = Object.keys(segment.query);
    const queryStringCollection = keys.map(
      (key) => `${key}=${segment.query![key]}`
    );
    const queryString = queryStringCollection.join("&");
    r += `?${queryString}`;
    return r;
  }
}
export interface IUrlSegment {
  /**菜单级别 */
  menuLevel: MenuLevel;
  /**页面路径 */
  pagePath?: string;
  // /**操作 */
  // action?: UrlAction
  /**查询参数 */
  query?: Record<string, string>;
}

/**菜单级别 */
export enum MenuLevel {
  /**PORTAL 一级目录 */
  portal = "/portal",

  /**项目 二级目录 */
  project = "/project",

  root = "/",
}
